package tech.aistar.service.impl;

import org.apache.ibatis.session.SqlSession;
import tech.aistar.mapper.UserMapper;
import tech.aistar.pojo.User;
import tech.aistar.service.IUserService;
import tech.aistar.util.MyBatisUtil;

import java.util.List;

/**
 * 本类用来演示: service层 - 主要职责是处理数据
 *
 *             service层调用mapper[dao层]
 *
 * @author: success
 * @date: 2021/4/15 4:26 下午
 */
public class UserServiceImpl implements IUserService {
    @Override
    public User login(String username) {
        //程序员重点写的代码..
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.findByUsername(username);

        MyBatisUtil.closeSqlSession(sqlSession);

        return user;
    }

    @Override
    public List<User> loadAll() {
        //程序员重点写的代码..
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.findAll();

        MyBatisUtil.closeSqlSession(sqlSession);
        return users;
    }
}
